package com.work.saturn.assets;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.utils.FullyLinearLayoutManager;
import com.work.saturn.R;
import com.work.saturn.bean.UserMsgDataBean;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @content 账单详情
 * @date 2018/7/11
 */
public class AssetsDetailActivity extends BaseHeadActivity {
    @BindView(R.id.assets_detail_list)
    RecyclerView mList;
    private List<UserMsgDataBean.HistoryBean> mHistoryBeanList;
    private String userId;
    private String symbol;
    private String num;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assets_detail);
        ButterKnife.bind(this);
        setTitle(getIntent().getStringExtra("name"));
        setTitleColor(R.color.colorYellow);
        setTitleImg(R.mipmap.headimg);
        mHistoryBeanList= (List<UserMsgDataBean.HistoryBean>)getIntent().getSerializableExtra("history");
        Collections.reverse(mHistoryBeanList);
        userId=getIntent().getStringExtra("userId");
        symbol=getIntent().getStringExtra("symbol");
        num=getIntent().getStringExtra("num");
        initData();
    }

    private void initData() {
        FullyLinearLayoutManager linearLayoutManagerTheme = new FullyLinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        View view= LayoutInflater.from(this).inflate(R.layout.assets_head, new LinearLayout(this), false);
        ((TextView)view.findViewById(R.id.assets_head)).setText(num);
        ((TextView)view.findViewById(R.id.assets_dw)).setText(symbol);
        linearLayoutManagerTheme.setOrientation(LinearLayoutManager.VERTICAL);
        mList.setLayoutManager(linearLayoutManagerTheme);
        AssetsHistoryDetailAdapter releaseDetailAdapter = new AssetsHistoryDetailAdapter();
        releaseDetailAdapter.addHeaderView(view);
        //设置适配器
        releaseDetailAdapter.bindToRecyclerView(mList);
    }

    @OnClick({R.id.assets_detail_in, R.id.assets_detail_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.assets_detail_in:
                Intent intent=new Intent(AssetsDetailActivity.this,AssetsTurnInActivity.class);
                intent.putExtra("userId",userId);
                startActivity(intent);
                break;
            case R.id.assets_detail_out:
                Intent intentout=new Intent(AssetsDetailActivity.this,AssetsTurnOutActivity.class);
                intentout.putExtra("userId",userId);
                intentout.putExtra("name",getIntent().getStringExtra("name"));
                startActivity(intentout);
                break;
        }
    }
    /**
     * 将时间戳转换为时间
     */
    public static String stampToDate(long s){
        String res;
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }
    /**
     * 资产历史列表
     */
    private class AssetsHistoryDetailAdapter extends BaseQuickAdapter<UserMsgDataBean.HistoryBean, BaseViewHolder> {
        AssetsHistoryDetailAdapter() {
            super(R.layout.item_assets_detail, mHistoryBeanList);
        }

        @Override
        protected void convert(BaseViewHolder helper, UserMsgDataBean.HistoryBean item) {
            helper.setImageResource(R.id.assets_type, userId.equals(item.getTo())?R.mipmap.add:R.mipmap.minus);
            helper.setText(R.id.assets_id, userId.equals(item.getTo())?item.getFrom():item.getTo());
            helper.setText(R.id.assets_time,stampToDate(item.getTimestamp()));
            helper.setText(R.id.assets_num, userId.equals(item.getTo())?("+"+item.getAmount()+"  "+symbol):("-"+item.getAmount()+"  "+symbol));
            helper.setTextColor(R.id.assets_num,userId.equals(item.getTo())?getResources().getColor(R.color.colorYellow):getResources().getColor(R.color.colorRed));
        }

    }


}
